package cc.eguid.tools.ui;

import cc.eguid.tools.RemoteEventListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

/**
 * 远程桌面显示组件面板
 */
public class RemoteDesktop extends JPanel {
    private BufferedImage desktopImage;
    private int lastPressedButton = -1;

    public RemoteDesktop() {
        setPreferredSize(new Dimension(640, 480));
        setBackground(Color.BLACK);
    }

    public void setDesktopImage(BufferedImage image) {
        this.desktopImage = image;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (desktopImage != null) {
            g.drawImage(desktopImage, 0, 0, getWidth(), getHeight(), this);
        }
    }

    // 添加鼠标和键盘事件监听
    public void addRemoteEventListener(RemoteEventListener listener) {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(desktopImage==null){
                    return;
                }
                double scaleX = (double) desktopImage.getWidth() / getWidth();
                double scaleY = (double) desktopImage.getHeight() / getHeight();
                int x = (int) (e.getX() * scaleX);
                int y = (int) (e.getY() * scaleY);
                lastPressedButton = e.getButton();
                listener.onMouseEvent("PRESSED", x, y, lastPressedButton);
                requestFocusInWindow();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if(desktopImage==null){
                    return;
                }
                double scaleX = (double) desktopImage.getWidth() / getWidth();
                double scaleY = (double) desktopImage.getHeight() / getHeight();
                int x = (int) (e.getX() * scaleX);
                int y = (int) (e.getY() * scaleY);
                listener.onMouseEvent("RELEASED", x, y, e.getButton());
                lastPressedButton = -1;
                requestFocusInWindow();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                requestFocusInWindow();
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                if(desktopImage==null){
                    return;
                }
                double scaleX = (double) desktopImage.getWidth() / getWidth();
                double scaleY = (double) desktopImage.getHeight() / getHeight();
                int x = (int) (e.getX() * scaleX);
                int y = (int) (e.getY() * scaleY);
                listener.onMouseEvent("MOVED", x, y, -1);
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                if(desktopImage==null){
                    return;
                }
                double scaleX = (double) desktopImage.getWidth() / getWidth();
                double scaleY = (double) desktopImage.getHeight() / getHeight();
                int x = (int) (e.getX() * scaleX);
                int y = (int) (e.getY() * scaleY);
                int button = lastPressedButton == -1 ? MouseEvent.BUTTON1 : lastPressedButton;
                listener.onMouseEvent("DRAGGED", x, y, button);
            }
        });

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                listener.onKeyEvent("PRESSED", e.getKeyCode(), e.getKeyChar());
            }

            @Override
            public void keyReleased(KeyEvent e) {
                listener.onKeyEvent("RELEASED", e.getKeyCode(), e.getKeyChar());
            }

            @Override
            public void keyTyped(KeyEvent e) {
                listener.onKeyEvent("TYPED", -1, e.getKeyChar());
            }
        });

        setFocusable(true);
        requestFocusInWindow();

        // 当组件显示到窗口后，自动请求焦点，确保键盘事件可用
        addHierarchyListener(e -> {
            if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && isShowing()) {
                SwingUtilities.invokeLater(this::requestFocusInWindow);
            }
        });
    }
}
